﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using BLL;
using Model;

namespace ProductionAnalysis.Web
{
    public partial class ProductManage_CustomerManage : System.Web.UI.Page
    {
        private Customer customer;

        public ProductManage_CustomerManage()
        {
            customer = new Customer();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCustomers();
            }
        }

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    CustomerInfo customerInfo = new CustomerInfo() { CustomerID = TextBox3.Text.Trim().ToString(), CustomerName = TextBox1.Text.Trim().ToString() };
        //    CustomerInfo cust = customer.LoadEntity(TextBox3.Text.Trim().ToString());
        //    if (cust.CustomerID == customerInfo.CustomerID)
        //    {
        //        Label3.Text = "客户编号已存在！";
        //        Label3.Visible = true;
        //        Label4.Visible = false;
        //    }
        //    else
        //    {
        //        customer.Insert(customerInfo);
        //        Label4.Text = "添加成功！";
        //        Label4.Visible = true;
        //        Label3.Visible = false;
        //        BindCustomers();
        //    }
        //}

        public void BindCustomers()
        {
            GridView1.DataSource = customer.LoadEntities();
            GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindCustomers();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //CustomerInfo customerInfo = new CustomerInfo(GridView1.DataKeys[e.RowIndex].Values[0].ToString(), GridView1.DataKeys[e.RowIndex].Values[1].ToString());
            string customerId = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
            customer.Delete(customerId);
            BindCustomers();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[2].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert")
            {
                GridView1.DataSource = null;
                GridView1.DataBind();
            }
        }

        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
            string customerID = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
            string customerName = ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text.Trim();
            CustomerInfo customerInfo = new CustomerInfo() { CustomerID = customerID, CustomerName = customerName };
            CustomerInfo cust = customer.LoadEntity(customerID);
            if (cust.CustomerID == customerInfo.CustomerID)
            {
                string str1 = "客户编号已存在！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            }
            else
            {
                customer.Insert(customerInfo);
                string str1 = "添加成功！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                BindCustomers();
            }
        }

        protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {
            BindCustomers();
        }
    }
}